package com.loan.controller;


import com.loan.entity.Authorize;
import com.loan.entity.Company;
import com.loan.entity.Result;
import com.loan.mapper.AuthorizeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 * @since 2024-09-28
 */
@RestController
@RequestMapping("/authorize")
public class AuthorizeController {

    @Resource
    RedisTemplate redisTemplate;

    @Autowired
    AuthorizeMapper authorizeMapper;

    @RequestMapping(value = "/getAuthorizeList",method = RequestMethod.POST)
    public List<Authorize> getAuthorizeList(){
        List<Authorize> authorizeList = authorizeMapper.selectList(null);
        return authorizeList;
    }

    @PostMapping("/addAuthorize")
    public Result addAuthorize(String resultImg,String partnerName,String partnerPhone){

        Date date = new Date();
        String str = "yyy-MM-dd HH:mm:ss";
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(str);
        String df = simpleDateFormat.format(date);


        Object uids = redisTemplate.opsForValue().get("uid");
        Integer uid = (Integer) uids;

        Authorize authorize=new Authorize();
        authorize.setUid(uid);
        authorize.setPartnerName(partnerName);
        authorize.setPartnerPhone(partnerPhone);
        authorize.setCommitTime(df);
        authorize.setResultImg("已签字");
        int count = authorizeMapper.insert(authorize);

        Result result = new Result();
        if (count>0){
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        return result;
    }

    @RequestMapping(value = "/getAuthorizeByUid",method = RequestMethod.POST)
    public Result getAuthorizeByUid(){
        Object uids = redisTemplate.opsForValue().get("uid");
        Integer uid = (Integer) uids;
        Map<String,Object> columnMap=new HashMap<>();
        columnMap.put("uid",uid);

        List<Authorize> authorizeList = authorizeMapper.selectByMap(columnMap);
        if (authorizeList.size()>0){
            Authorize authorize=authorizeList.get(0);
            Result result = new Result();
            result.setSuccess(true);
            result.setObj(authorize);
            return result;
        }

        Result result = new Result();
        result.setSuccess(false);
        return result;
    }

    @RequestMapping(value = "/deleteAuthorizeByUid",method = RequestMethod.POST)
    public Result deleteAuthorizeByUid(){
        Object uids = redisTemplate.opsForValue().get("uid");
        Integer uid = (Integer) uids;
        Map<String,Object> columnMap=new HashMap<>();
        columnMap.put("uid",uid);

        int i = authorizeMapper.deleteByMap(columnMap);
        if (i>0){
            Result result = new Result();
            result.setSuccess(true);
            return result;
        }
        Result result = new Result();
        result.setSuccess(false);
        return result;
    }

}

